package com.demo.userprofile.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo.userprofile.component.service.SayHelloService;
import com.demo.userprofile.component.vo.DolphinSchedulerProject;
import com.demo.userprofile.component.service.DolphinSchedulerHttpApi;
import com.demo.userprofile.component.service.HiveService;
import com.demo.userprofile.component.service.MySQLTestService;
import com.demo.userprofile.component.service.OssService;
import com.demo.userprofile.component.service.RedisService;
import com.demo.userprofile.sdk.SayHelloServiceClient;

/**
 * userprofile-api简单的API测试接口
 *
 * @author userprofile_demo
 */
@RestController
public class SayHelloController {

    @Autowired
    private SayHelloService sayHelloService;

    @Autowired
    private SayHelloServiceClient sayHelloServiceClient;

    @Autowired
    private MySQLTestService mySQLTestService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private OssService ossService;

    @Autowired
    private HiveService hiveService;

    @Autowired
    private DolphinSchedulerHttpApi dolphinSchedulerHttpApi;

    // 普通服务接口调用
    @ResponseBody
    @RequestMapping("/hello")
    public String sayHello() {
        return sayHelloService.getWords();
    }

    // SpringCloud服务接口调用
    @ResponseBody
    @RequestMapping("/hellov2")
    public String sayHello2(String name) {
        return sayHelloServiceClient.getWordsFromService(name);
    }

    // 添加内容到数据库中
    @ResponseBody
    @RequestMapping("/addTestItem")
    public Object addTestItem(String name) {
        return mySQLTestService.addTestItem(name);
    }

    // 测试Redis相关操作
    @ResponseBody
    @RequestMapping("/setGetRedis")
    public Object setGetRedis(String keyValue) {
        redisService.set("abc", keyValue);
        return redisService.get("abc");
    }

    // 测试上传文件到OSS
    @ResponseBody
    @RequestMapping("/uploadOss")
    public Object uploadOss(String fileName, String content) {
        return ossService.uploadFile(content.getBytes(), fileName);
    }

    // 测试连接Hive代码
    @ResponseBody
    @RequestMapping("/testHive")
    public String testHive() {
        hiveService.testHive();
        return "success";
    }

    // 调用DolphinScheduler相关接口
    @ResponseBody
    @RequestMapping("/dolphin/projects")
    public Object getProjects(String token) {
        // 其中token需要在dolphinscheduler上申请
        DolphinSchedulerProject projectList = dolphinSchedulerHttpApi.getAllProjects(token);
        return projectList.getData().size();
    }
}
